<?php

declare (strict_types=1);

namespace App\Application\Admin\Model;

use App\Application\Admin\Service\AccessService;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $tiktok_id
 * @property string $client_key
 * @property string $client_secret
 * @property int $expires_in
 * @property string $access_token
 * @property int $store_id
 * @property int $admin_id
 * @property string $poi_id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property-read \Hyperf\Database\Model\Collection|self[] $children
 */
class Tiktok extends Model
{
    use SoftDeletes;

    protected string $primaryKey = 'tiktok_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'tiktok';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = ['tiktok_id', 'client_key', 'client_secret', 'expires_in', 'access_token', 'store_id','poi_id','admin_id'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected array $casts = [
        'tiktok_id' => 'integer',
        'client_key' => 'string',
        'client_secret' => 'string',
     //   'expires_in' => 'integer',
     //   'access_token' => 'string',
        'store_id' => 'integer',
        'admin_id' => 'integer',
        'poi_id'=>'string',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];


    /**
     * 创建门店
     *
     * @return bool
     * @throws ErrorException
     */
    static function createTiktok(int $tiktok_id, array $tiktok = []): self
    {
        Db::beginTransaction();
        $tiktok = self::updateOrCreate(['tiktok_id' => $tiktok_id], $tiktok);
        Db::commit();
        return $tiktok;

    }

}